package com.choudou5.solr.framework.holder;

import com.choudou5.solr.framework.constants.SysConsts;
import com.choudou5.solr.util.CookieUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * @Name：RequestContextHolder 说明
 * @@author choudou5
 * @@date 2018-07-24
 */
public class RequestContextHolder {

    private static final ThreadLocal<HttpServletRequest> reqHolder = new NamedThreadLocal("Request object");

    public RequestContextHolder() {
    }

    public static void resetRequest() {
        reqHolder.remove();
    }

    public static void setRequest(HttpServletRequest request) {
        reqHolder.set(request);
    }


    public static String getSessionId() {
        HttpServletRequest req = reqHolder.get();
        if(req == null)
            return null;
        return req.getSession().getId();
    }

    public static String getToken() {
        HttpServletRequest req = reqHolder.get();
        if(req == null)
            return null;
        return CookieUtil.getCookie(req, SysConsts.HEADER_TOKEN);
    }

}
